package dao.impl;

import dao.ISalaryAdministrationDao;
import pojo.Employees;
import pojo.Salaries;
import pojo.SalaryDTO;
import util.DBHelper;
import util.PageHelper;

import java.util.ArrayList;

public class SalaryAdministrationDao implements ISalaryAdministrationDao {
    private DBHelper db = new DBHelper();

    @Override
    public int updateSalary(Salaries salaries, Integer eId) {
        String sql = "update salaries set effectiveDate=?, baseSalary=?, bonus=?, benefits=?, insurance=?, tax=?, sDescription=? where eId=?";
        Object[] obj = {salaries.getEffectiveDate(), salaries.getBaseSalary(), salaries.getBonus(), salaries.getBenefits(), salaries.getInsurance(), salaries.getTax(), salaries.getsDescription(), eId};
        return db.dtUpdate(sql, obj);
    }

    @Override
    public ArrayList<Salaries> selectSalary(Integer eId) {
        String sql = "select * from salaries s, employees e where s.eId=e.eId and s.eId=?";
        return db.dtQuery(sql, Salaries.class, eId);
    }


    @Override
    public int getTotalCount(Salaries salaries, Employees employees) {
        StringBuilder sql = new StringBuilder("SELECT COUNT(*) FROM salaries s INNER JOIN employees e ON s.eId = e.eId WHERE 1=1");
        ArrayList<Object> list = new ArrayList<>();

        if (employees != null && employees.geteName() != null && !employees.geteName().isEmpty()) {
            sql.append(" AND e.eName LIKE ?");
            list.add("%" + employees.geteName() + "%");
        }

        if (salaries != null && salaries.getEffectiveDate() != null) {
            sql.append(" AND s.effectiveDate LIKE ?");
            list.add("%" + salaries.getEffectiveDate() + "%");
        }

        // 修正部门ID查询
        if (employees != null && employees.getDepartmentId() != -1) {
            sql.append(" AND e.departmentId = ?");
            list.add(employees.getDepartmentId());
        }

        try {
            System.out.println("SQL: " + sql.toString());
            System.out.println("Parameters: " + list);

            Object result = db.executeScalar(sql.toString(), list.toArray());
            return result != null ? Integer.parseInt(result.toString()) : 0;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
    @Override
    public int insertSalaries(Salaries salaries) {
        String sql = "INSERT INTO salaries VALUES(null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        Object[] obj = {
                salaries.geteId(),
                salaries.getEffectiveDate(),
                salaries.getBaseSalary(),
                salaries.getBonus(),
                salaries.getBenefits(),
                salaries.getInsurance(),
                salaries.getTax(),
                salaries.getsDescription(),
                salaries.getsCreatedAt(),
                salaries.getsUpdatedAt()
        };
        return db.dtUpdate(sql, obj);
    }
    @Override
    public ArrayList<SalaryDTO> selectSalaries(Salaries salaries, Employees employees, PageHelper pageHelper) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT ");
        sql.append("e.eId, e.eName, e.departmentId, e.positionId, ");
        sql.append("s.sId, s.effectiveDate, s.baseSalary, s.bonus, s.benefits, ");
        sql.append("s.insurance, s.tax, s.sDescription, s.sCreatedAt, s.sUpdatedAt ");
        sql.append("FROM salaries s INNER JOIN employees e ON s.eId = e.eId WHERE 1=1");

        ArrayList<Object> list = new ArrayList<>();

        if (employees != null && employees.geteName() != null && !employees.geteName().isEmpty()) {
            sql.append(" AND e.eName LIKE ?");
            list.add("%" + employees.geteName() + "%");
        }

        if (salaries.getEffectiveDate() != null) {
            sql.append(" AND s.effectiveDate LIKE ?");
            list.add("%" + salaries.getEffectiveDate() + "%");
        }
        if (employees != null && employees.getDepartmentId() != -1) {
            sql.append(" AND e.departmentId = ?");
            list.add(employees.getDepartmentId());
    }

        sql.append(" LIMIT ?, ?");
        list.add(pageHelper.getStartIndex());
        list.add(pageHelper.getShowNumber());

        return db.dtQuery(sql.toString(), SalaryDTO.class, list.toArray());
    }
}